﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 串口模板
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        Thread t;
        public serial serial = new serial();
        bool NRB_flag = false;


        private void button1_Click(object sender, EventArgs e)
        {
            serial.ShowDialog();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serial.serialPort1.DataReceived += serialPort1_DataReceived;
            try
            {
                serial.serialPort1.PortName = Properties.Settings.Default.PortName;
                serial.serialPort1.BaudRate = Convert.ToInt32(Properties.Settings.Default.BaudRate);
                serial.serialPort1.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //t = new Thread(showtxt);
            //t.Start();.
            //textBox3.Text = serial.serialPort1.WriteBufferSize.ToString();
        }

        string SerialReceivedStr = null;
        string ReceivedData = null;
        bool ReceiveFlag = false;
        string CompareString;

        public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                SerialReceivedStr = serial.serialPort1.ReadTo("OK\r\n");
                textBox3.AppendText(SerialReceivedStr);
                //if (SerialReceivedStr.Contains(CompareString))
                //{
                //    ReceiveFlag = true;
                //}
            }
            catch
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.SerialWriteEvent += Form2_SerialWriteEvent;
            form2.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (serial.serialPort1.IsOpen)
            {
                serial.serialPort1.Write(textBox1.Text + "\r\n");
            }
            else
            {
                MessageBox.Show("请打开串口！");
            }
        }

        public void Form2_SerialWriteEvent(string s)
        {
            if (!serial.serialPort1.IsOpen)
            {
                serial.serialPort1.Open();                             
            }
            serial.serialPort1.Write(s);
        }

        int NB_AT(string buf, string buf1, int timeout)
        {
            serial.serialPort1.Write(buf + "\r\n");          
            int time = 0;
            while (time < timeout)
            {
                try
                {
                    SerialReceivedStr = serial.serialPort1.ReadTo("\r\n");
                    textBox3.AppendText(SerialReceivedStr);
                    if (SerialReceivedStr.Contains(buf1))
                    {
                        break;
                    }
                    Thread.Sleep(100);
                    time++;
                }
                catch
                {
                }
            }
            if (time >= timeout)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
       
        int NB_Connect()
        {
            int res;
            int time = 40;

            res = NB_AT("AT+NRB", "Neul", 80);
            //serial.serialPort1.Write("AT+NRB\r\n");

            //Thread.Sleep(8000);
            res = NB_AT("AT+CFUN=0", "OK", 10);
            if (res != 0)
            {
                return 3;
            }
            res = NB_AT("AT+NCDP=112.93.129.154,5683", "OK", 10);
            if (res != 0)
            {
                return 4;
            }
            res = NB_AT("AT+CFUN=1", "OK", 200);
            if (res != 0)
            {
                return 5;
            }
            res = NB_AT("AT+CGATT=1", "OK", 10);
            if (res != 0)
            {
                return 6;
            }
            res = 1;
            while (res == 1) //获取IP
            {
                res = NB_AT("AT+CGPADDR", ".", 10);
                time--;
                if (time == 0)
                {
                    return 7;
                }
            }
            res = NB_AT("AT+NNMI=1", "OK", 200);
            if (res != 0)
            {
                return 8;
            }
            return 0;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
            int res = NB_Connect();
            if (res == 0)
            {
                MessageBox.Show("NB OK");
            }
            else
            {
                MessageBox.Show("ERR" + res);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            serial.serialPort1.Write("AT\r\n");
        }

        private void textBox3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            textBox3.Text = "";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int res;
            res = NB_AT("AT+CFUN=0", "OK", 50);
            res = 0;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            serial.serialPort1.Write("AT+CFUN=0\r\n");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            serial.serialPort1.Write("AT+NCDP=112.93.129.154,5683\r\n");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            serial.serialPort1.Write("AT+CFUN=1\r\n");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            serial.serialPort1.Write("AT+CGATT=1\r\n");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            int res;
            res = NB_AT("AT+NRB", "OK", 130);
            textBox2.Text = res.ToString();

        }

        private void button12_Click(object sender, EventArgs e)
        {
            serial.serialPort1.Write("AT+CGSN=1\r\n");
        }
    }
}
